package com.jt.service;

import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll(User user) {
        return userMapper.findAll(user);
    }

    @Override
    public String login(User user) {
        //把前端传过来的user对象中的密码进行加密并赋值给user对象
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        //把加密后的user对象 映射给mapper接口  进行数据库查询  查到就返回查到的user对象(说明有该用户)
        User userDB = userMapper.findUserByUP(user);
        if (userDB == null){
            return null;
        }
        String uuid = UUID.randomUUID().toString().replace("-", "");
        return uuid;
    }

    @Override
    public Object getUserList(PageResult pageResult) {
        String query = pageResult.getQuery();
        long total = userMapper.getTotal(query);
        int size = pageResult.getPageSize();
        int start = (pageResult.getPageNum() - 1) * size;
        List<User> rows = userMapper.getUserRows(start,size,query);
        return pageResult.setTotal(total).setRows(rows);
    }

    @Override
    public void updateStatus(User user) {
        user.setUpdated(new Date());
        userMapper.updateStatus(user);
    }

    @Override
    public void addUser(User user) {
        user.setStatus(true)
            .setCreated(new Date())
            .setUpdated(user.getCreated());
        userMapper.addUser(user);
    }

    @Override
    public User findUserById(User user) {
        return userMapper.findUserById(user);
    }

    @Override
    public Integer updateUser(User user) {
        user.setUpdated(new Date());
        return userMapper.updateUser(user);
    }

    @Override
    public Integer delectUserById(Integer id) {
        return userMapper.delectUserById(id);
    }
}
